package com.myway.service.ibe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.myway.dto.domain.PnrVo;
import com.myway.dto.domain.Segment;
import com.myway.dto.vo.FlySegmentVo;
import com.myway.service.ibe.command.PNRCommand;
import com.myway.service.ibe.command.ReservePNRCommand;
import com.travelsky.ibe.client.pnr.RTResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yufei on 2018/6/7.
 */
@Controller
@RequestMapping("/pnr")
public class PNRController {

    @Autowired
    private ReservePNRCommand reservePNRCommand;

    @Resource(name="pnrCommand")
    private PNRCommand pnrCommand;

    /**
     * 取消PNR
     */
    @RequestMapping("/cancel")
    @ResponseBody
    public String cancel( @RequestBody String pnrNo){
        String result = pnrCommand.cancelPnr(pnrNo);
        return result;
    }
    /**
     * 提取PNR
     */
    @RequestMapping("/getpnr")
    @ResponseBody
    public String getPnr( @RequestBody String pnrNo) throws Exception{
        String result = pnrCommand.getPnrByPnrNo(pnrNo);
        return result;
    }
    /**
     * 查看PNR是否存在
     */
    @RequestMapping("/exist")
    @ResponseBody
    public boolean isPnrAvailable( @RequestBody String pnrNo) throws Exception{
        boolean result = pnrCommand.isPnrAvailable(pnrNo);
        return result;
    }

    /**
     * pnr预定
     */
    @RequestMapping("/seat")
    @ResponseBody
    public String test2( @RequestBody String json){

        PnrVo pnrVo = JSON.parseObject(json,new TypeReference<PnrVo>(){});
        Map params = new HashMap();
        params.put("segments",pnrVo.getSegments());
        params.put("passenges",pnrVo.getPassenges());
        params.put("ticketPrice",pnrVo.getTicketPrice());
        params.put("flyOrder",pnrVo.getFlyOrder());
        params = reservePNRCommand.execute(params);
        return JSON.toJSONString(params);
    }
    /**
     * @名称：reconfirm
     * @描述：RR PNR中的所有航段
     * @param pnrNo
     * @param segments
     */
    @RequestMapping("/reconfirm")
    public void reconfirm( String pnrNo, List<FlySegmentVo> segments) throws Exception{
        pnrCommand.reconfirm(pnrNo,segments);
    }
}
